from rest_framework.generics import ListAPIView
from rest_framework.permissions import DjangoModelPermissions, IsAuthenticated
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from apps.meiduo_admin.serializers.sku_serializers import SKUSerializer, SKUCategorieSerializer, GoodsSimpleSerializer, \
    GoodsSpecSerializer
from apps.meiduo_admin.utils import PageNum


class SKUModelViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    # 分页
    pagination_class = PageNum

    # 添加权限限制  DjangoModelPermissions对模型类权限限制
    permission_classes = [IsAuthenticated,DjangoModelPermissions]


class SKUCategoriesView(ListAPIView):
    serializer_class = SKUCategorieSerializer
    # 根据数据存储规律parent_id大于37为三级分类信息，查询条件为parent_id__gt=37
    # queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    # 或者
    queryset = GoodsCategory.objects.filter(subs=None)


# 获取sku表名称数据
class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = SPU.objects.all()


class GoodsSpecView(ListAPIView):
    serializer_class = GoodsSpecSerializer

    def get_queryset(self):
        pk = self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)
